package cloud.seckill.seckillserver.controller;

import cloud.seckill.seckillserver.model.result.Result;
import cloud.seckill.seckillserver.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @title: SeckillController
 * @Author Wangsj
 * @Date: 2021/5/24
 */
@RestController
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * @Author Wangsj
     * @Description 级别一，未加任何锁，会出现线程安全，无法实现秒杀
     * @Date 11:12 2021/5/25
     * @Param []
     * @return cloud.seckill.seckillserver.model.result.Result
     **/
    @GetMapping("buy/lv1")
    public Result buy_lv1(){
        return seckillService.seckillLv1();
    }

    /**
     * 级别二，单机锁，会出现超卖
     * @return
     */
    @GetMapping("buy/lv2")
    public Result buy_lv2(){
        return seckillService.seckillLv2();
    }


    /**
     * 级别3，Redis锁，使用setIfAbsent加锁
     * @return
     */
    @GetMapping("buy/lv3")
    public Result buy_lv3() throws InterruptedException {
        return seckillService.seckillLv3();
    }

    /**
     * 级别3，Redis锁，使用setIfAbsent加锁
     * @return
     */
    @GetMapping("buy/lv4")
    public Result buy_lv4() throws Exception {
        return seckillService.seckillLv4();
    }

    /**
     * 级别3，Redis锁，使用setIfAbsent加锁
     * @return
     */
    @GetMapping("buy/lv5")
    public Result buy_lv5() {
        return seckillService.seckillLv5();
    }
}
